<?php
/**
 * Created by PhpStorm.
 * User: Bzing
 * Date: 2017/6/23
 * Time: 22:08
 */
namespace app\api\controller;
use app\models\Models;

class User extends Base {
    /**
     * 用户详情
     * @return \think\response\Json
     */
    public function userShow(){
        if(request()->isPost()){
            try{
                $params = self::paramsSign(['equipment','encrypt','user_id']);
                $is_follow = false;
                if($params['token']){
                    // 登录验证
                    $params = self::checkLogin();
                    // 查询用户关注信息
                    $follow = new Models('follow');
                    $is_follow = $follow->_find(['follow_id'=>$params['user_id'],'fans_id'=>$params['uid'],'follow_object'=>Models::FOLLOW_USER]);
                }
                // 获取用户资料
                $info = self::getUserDetail(['uid'=>$params['user_id']]);
                $info['is_follow'] = $is_follow ? Models::FOLLOW_YES : Models::FOLLOW_NOT;
            } catch (\Exception $e){
                return json(self::formatResult($e->getCode(),$e->getMessage()));
            }
            return json(self::formatSuccessResult($info));
        }
    }

    /**
     * 修改头像
     * @return \think\response\Json
     */
    public function updatePortrait(){
        if(request()->isPost()){
            try{
                // 登录验证
                $params = self::checkLogin(['equipment','encrypt']);
                if(!$_FILES){
                    throw new \LogicException('上传图片不能为空',1033);
                }
                $user = new Models('user');
                $bool = $user->_save(['id'=>$params['uid'],'portrait'=>upload('portrait')[0]]);
                if(!$bool){
                    throw new \LogicException('操作失败',1010);
                }
            } catch (\Exception $e){
                return json(self::formatResult($e->getCode(),$e->getMessage()));
            }
            return json(self::formatSuccessResult());
        }
    }

    /**
     * 默认销售
     * @return \think\response\Json
     */
    public function activitySeller(){
        if(request()->isPost()){
            $params = input('param.');
            try{
                // 验证
                self::sign($params);
                // 营销信息
                $list = self::seller($params);
                $info = $list[0];
                unset($info['birth'],$info['sales_amount']);
            } catch (\Exception $e){
                return json(self::formatResult($e->getCode(),$e->getMessage()));
            }
            return json(self::formatSuccessResult($info));
        }
    }

    /**
     * 销售列表
     * @return \think\response\Json
     */
    public function sellerList(){
        if(request()->isPost()){
            $params = input('param.');
            try{
                // 登录验证
                self::sign($params);
                // 营销信息
                $list = self::seller($params);
            } catch (\Exception $e){
                return json(self::formatResult($e->getCode(),$e->getMessage()));
            }
            return json(self::formatSuccessResult($list));
        }
    }

    /**
     * 营销信息
     * @param $params
     * @return false|\PDOStatement|string|\think\Collection]
     */
    function seller($params){
        $user = new Models('user');
        $field  = 'a.id,a.nickname,a.sex,a.birth,a.portrait,a.signature,a.wine,b.sales_amount';
        $where  = ['a.status'=>Models::STATUS_ON,'a.type_id'=>Models::USER_TYPE_SELLER,'a.up_id'=>$params['business_id']];
        $list = $user->joinMany([['account b','a.id = b.uid','left']],$where,$field);
        foreach($list as $k=>$v){
            $list[$k]['portrait'] = empty($v['portrait']) ? '' : self::arrayMapPortrait($v['portrait']);
            $list[$k]['age']      = calcAge($v['birth']);
        }
        $sales_amount = array_column($list,'sales_amount');   // 获取列数据
        array_multisort($sales_amount,SORT_DESC,$list);       // 多维数组的排序
        return $list;
    }

    /**
     * 艺人列表
     * @return \think\response\Json
     */
    public function actorList(){
        if(request()->isPost()){
            try{
                $params = self::paramsSign(['equipment','encrypt','business_id']);
                if($params['token']){
                    // 登录验证
                    $params = self::checkLogin();
                    // 查询用户关注信息的id
                    $follow = new Models('follow');
                    $list = $follow->_select(['fans_id'=>$params['uid'],'follow_object'=>Models::FOLLOW_USER,'status'=>Models::FOLLOW_YES],'id');
                    $ids  = array_column($list,'id');
                }
                // 艺人信息
                $user = new Models('user');
                $field = 'a.id,a.nickname,a.portrait,b.name type_name,a.click_num,a.fans_num';
                $where = ['a.status'=>Models::STATUS_ON,'a.type_id'=>Models::USER_TYPE_ACTOR,'a.up_id'=>$params['business_id'],'b.status'=>Models::STATUS_ON];
                $list = $user->joinMany([['user_actor_type b','a.actor_id = b.id','left']],$where,$field);
                foreach($list as $k=>$v){
                    $list[$k]['portrait']  = empty($v['portrait']) ? '' : self::arrayMapPortrait($v['portrait']);
                    $list[$k]['hot']       = $v['click_num'] + $v['fans_num'];
                    $list[$k]['is_follow'] = empty($ids) ? Models::FOLLOW_NOT : (in_array($v['id'],$ids) ? Models::FOLLOW_YES : Models::FOLLOW_NOT);
                    unset($list[$k]['click_num']);
                }
                $hot = array_column($list,'hot');   // 获取列数据
                array_multisort($hot,SORT_DESC,$list);       // 多维数组的排序
            } catch (\Exception $e){
                return json(self::formatResult($e->getCode(),$e->getMessage()));
            }
            return json(self::formatSuccessResult($list));
        }
    }
}